#!/bin/bash

BACADIR=/home/tiago/werk/baca
BINDIR=$BACADIR/bin
SENDER=somebody@acme.com
TOPURL=http://your.host.edu/baca

#Parameters: [-a] [-g] [-t] [-r] [-s] [-o] [-d] <-q GenBank query> [-e email]
# -a analyze (otherwise also retrieve )
# -g gene
# -t tRNA
# -r rRNA
# -s stem loop
# -o origin
# -d D-loop
# -q <query> query
# -e <email> email address

CMD=retrieve
GENE=no
TRNA=no
RRNA=no
STEMLOOP=no
ORIGIN=no
DLOOP=no
VERT=no
EMAIL = ""
#args=`getopt -o :agtrsodvq:e: -- "$@"`

#set -- $args
while getopts ":agtrsodvq:e:" flag
do
  case "$flag" in
        a) CMD=analyze;;
        g) GENE=yes;;
        t) TRNA=yes;;
        r) RRNA=yes;;
        s) STEMLOOP=yes;;
        o) ORIGIN=yes;;
        d) DLOOP=yes;;
        v) VERT=yes;;
        q) QUERY=$OPTARG;;
        e) EMAIL=$OPTARG;;
  esac
done

echo BACA is running, please hit the reload button every 30 seconds... > readme.txt
if test $CMD != "analyze"; then
    mkdir data
    cd data
    #as list can be long we do like this
    for i in *; do rm "$i"; done 
    echo Searching $QUERY...
    perl $BINDIR/search.pl genome "$QUERY" | tee genome.list
    
    echo Retrieving $QUERY...
    cat genome.list | perl $BINDIR/retrieve_features.pl  genome $GENE $TRNA $RRNA $STEMLOOP $ORIGIN $DLOOP| tee genome_id.list;
else
    cd data
fi

echo Aggregating by genome...
rm GENOME*
for i in `cat genome_id.list`; do
    echo $i;
    ls $i*.fasta > todo;
    perl $BINDIR/join_fastas.pl GENOME_$i.fasta < todo;
    perl $BINDIR/redundant_stats.pl $VERT GENOME_$i.fasta > GENOME_$i.stats COX1;
done;


if test $GENE = "yes"; then
    echo Aggregating by gene...
    rm GENE*
    ls *cDNA*fasta | perl  -e "while (<>) {print \$1 . \"\\n\" if /#(.+).fasta/;}" | sort | uniq > gene.list
    for i in `cat gene.list`; do
	echo $i;
	ls *"$i".fasta > todo ;
	perl $BINDIR/join_fastas.pl "GENE_$i.fasta" < todo;
	perl $BINDIR/redundant_stats.pl $VERT "GENE_$i.fasta" > GENE_$i.stats;
    done;
fi

if test $TRNA = "yes"; then
    echo Aggregating by tRNA...
    rm TRNA*
    ls *tRNA*fasta | perl  -e "while (<>) {print \$1 . \"\\n\" if /#(.+).fasta/;}" | sort | uniq > trna.list
    for i in `cat trna.list`; do
	echo $i;
	ls *$i.fasta > todo ;
	perl $BINDIR/join_fastas.pl "TRNA_$i.fasta" < todo;
    done;
fi

if test $RRNA = "yes"; then
    echo Aggregating by rRNA...
    rm RRNA*
    ls *rRNA*fasta | perl  -e "while (<>) {print \$1 . \"\\n\" if /#(.+).fasta/;}" | sort | uniq > rrna.list
    for i in `cat rrna.list`; do
	echo "$i";
	ls *"$i".fasta > todo ;
	perl $BINDIR/join_fastas.pl "RRNA_$i.fasta" < todo;
    done;
fi

if test $STEMLOOP = "yes"; then
    echo Aggregating by stem loop...
    ls *stem_loop*fasta | perl  -e "while (<>) {print \$1 . \"\\n\" if /#(.+).fasta/;}" | sort | uniq > stem.list
    rm STEM*
    for i in `cat stem.list`; do
	echo $i;
	ls *"$i".fasta > todo ;
	perl $BINDIR/join_fastas.pl "STEM_$i.fasta" < todo;
    done;
fi

if test $ORIGIN = "yes"; then
    echo Aggregating by replication origin...
    rm REP_*
    ls *rep_origin*fasta | perl  -e "while (<>) {print \$1 . \"\\n\" if /#(.+).fasta/;}" | sort | uniq > rep.list
    for i in `cat rep.list`; do
	echo "$i";
	ls *"$i".fasta > todo ;
	perl $BINDIR/join_fastas.pl "REP_$i.fasta" < todo;
    done;
fi

if test $DLOOP = "yes"; then
    echo Aggregating by D-Loop...
    rm DL*
    ls *D-loop*fasta | perl  -e "while (<>) {print \$1 . \"\\n\" if /#(.+).fasta/;}" | sort | uniq > dloop.list
    for i in `cat dloop.list`; do
	echo $i;
	ls *"$i".fasta > todo ;
	perl $BINDIR/join_fastas.pl "DL_$i.fasta" < todo;
    done;
fi


for i in * ; do zip -g baca.zip $i; done

rm -rf ../R
rm -rf ../html
mkdir ../R
mkdir ../html

echo Generating SVG
cat genome_id.list | perl -I/home/tiago/perl/share/perl/5.8.7 $BINDIR/generate_map.pl > ../html/map.svg
convert ../html/map.svg ../html/map.png
cat genome_id.list | perl -I/home/tiago/perl/share/perl/5.8.7 $BINDIR/generate_imagemap.pl > ../html/map.html
cd ..
perl $BINDIR/generate_r.pl COX1

cp $BACADIR/web/index-top.html html/index.html
cp $BACADIR/web/index-frame-ie.html html
cp $BACADIR/web/index-frame-simple.html html
cp $BACADIR/web/index-frame.html html
cp $BACADIR/web/menu.html html
cp $BACADIR/web/menu-ie.html html
cp $BACADIR/web/over.html html
perl $BINDIR/generate_html.pl "$1"

for i in data/*fasta ; do rm $i; done
for i in data/*gbk ; do rm $i; done
for i in data/*meta ; do rm $i; done
for i in data/*stats ; do rm $i; done
#for i in data/*list ; do rm $i; done
for i in data/todo ; do rm $i; done

echo Final analysis and mail

if test $EMAIL != ""; then
    perl $BINDIR/mail.pl  "$QUERY" "$EMAIL" "$SENDER" "$TOPURL"
fi
perl $BINDIR/final.pl  "$QUERY" >> html/index.html

echo BACA is READY... please enter the html directory > readme.txt
